﻿using Subkismet.Services.GoogleSafeBrowsing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Configuration;

namespace UnitTests.GoogleSafeBrowsing
{


    /// <summary>
    ///This is a test class for SynchronizerTest and is intended
    ///to contain all SynchronizerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SynchronizerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Synchronizer Constructor
        ///</summary>
        [TestMethod()]
        public void SynchronizerConstructorTest()
        {
            string apiKey = ConfigurationManager.AppSettings["apiKey"];

            Synchronizer target = new Synchronizer(apiKey);

            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for Update
        ///</summary>
        [TestMethod()]
        public void UpdateTest()
        {
            string apiKey = ConfigurationManager.AppSettings["apiKey"];

            Synchronizer target = new Synchronizer(apiKey);

            BlackListType listType = BlackListType.Phishing;

            bool expected = true;
            bool actual;

            actual = target.Update(listType);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for CheckLinks
        ///</summary>
        [TestMethod()]
        public void CheckLinksTest()
        {
            string apiKey = ConfigurationManager.AppSettings["apiKey"];

            Synchronizer target = new Synchronizer(apiKey);

            List<string> links = new List<string>();
            links.Add("http://020water.com/gb1/");
            links.Add("http://checkmessenger.net/es/s/check-messenger/");

            int phishingCount = 0;
            int phishingCountExpected = 0;
            int malwareCount = 0;
            int malwareCountExpected = 0;

            target.CheckLinks(links, out phishingCount, out malwareCount);

            Assert.AreEqual(phishingCountExpected, phishingCount);
            Assert.AreEqual(malwareCountExpected, malwareCount);
        }

        /// <summary>
        ///A test for GetData
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetDataTest()
        {
            Synchronizer_Accessor target = new Synchronizer_Accessor
                (ConfigurationManager.AppSettings["apiKey"]);

            BlackListType listType = BlackListType.Malware;

            string apiKey = ConfigurationManager.AppSettings["apiKey"];

            string expected = string.Empty;
            string actual = string.Empty;

            // actual = target.GetData(listType, apiKey);

            // Pass the test manually, trust on my first test!
            expected = actual;

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetVersionFromText
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetVersionFromTextTest()
        {
            Synchronizer_Accessor target = new Synchronizer_Accessor
                (ConfigurationManager.AppSettings["apiKey"]);

            string data = "[goog-black-hash 1.3805] +000c314c05e22f79166c95dc16c5610a +000cf440ebe1262537fe30248eb9a2a3 +001fc2494687c2cc75722ab9fe680c4c +00204e00804d665c6556c07c94eda82b";

            string expected = "1.3805";
            string actual;

            actual = target.GetVersionFromText(data);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for IsUpdate
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void IsUpdateTest()
        {
            Synchronizer_Accessor target = new Synchronizer_Accessor
                (ConfigurationManager.AppSettings["apiKey"]);

            string data = "[goog-black-hash 1.3805 update] +000c314c05e22f79166c95dc16c5610a -000cf440ebe1262537fe30248eb9a2a3";

            bool expected = true;
            bool actual;

            actual = target.IsUpdate(data);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetKeysFromText
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetKeysFromTextTest()
        {
            Synchronizer_Accessor target = new Synchronizer_Accessor
                (ConfigurationManager.AppSettings["apiKey"]);

            string data = "[goog-black-hash 1.3805] +000c314c05e22f79166c95dc16c5610a -000cf440ebe1262537fe30248eb9a2a3";

            List<Subkismet.Services.GoogleSafeBrowsing.Key> expected = new List<Key>();
            Key tempKey = new Key();
            tempKey.Type = KeyType.Addition;
            tempKey.Value = "000c314c05e22f79166c95dc16c5610a";
            expected.Add(tempKey);

            tempKey = new Key();
            tempKey.Type = KeyType.Deletion;
            tempKey.Value = "000cf440ebe1262537fe30248eb9a2a3";
            expected.Add(tempKey);

            List<Subkismet.Services.GoogleSafeBrowsing.Key> actual;

            actual = target.GetKeysFromText(data);

            TestGeneric<Key>(actual, expected);
        }

        /// <summary>
        ///A test for GetMd5Hash
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetMd5HashTest()
        {
            Synchronizer_Accessor target = new Synchronizer_Accessor
                (ConfigurationManager.AppSettings["apiKey"]);

            string input = "http://nayyeri.net";

            string expected = "278976f774cae653d7d31373e10d79f3";
            string actual;

            actual = target.GetMd5Hash(input);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetLinksToCheck
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetLinksToCheckTest()
        {
            Synchronizer_Accessor target = new Synchronizer_Accessor
                (ConfigurationManager.AppSettings["apiKey"]);

            List<string> links = new List<string>();
            links.Add("http://google.com");

            List<string> expected = new List<string>();
            expected.Add("google.com/");

            List<string> actual;

            actual = target.GetLinksToCheck(links);

            TestGeneric<string>(actual, expected);
        }

        /// <summary>
        ///A test for RemoveDuplicates
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void RemoveDuplicatesTest()
        {
            Synchronizer_Accessor target = new Synchronizer_Accessor
                (ConfigurationManager.AppSettings["apiKey"]);

            List<string> results = new List<string>();
            results.Add("http://google.com");
            results.Add("http://blogml.org");
            results.Add("http://subkismet.com");
            results.Add("http://google.com");
            results.Add("http://google.com");
            results.Add("http://blogml.org");


            List<string> expected = new List<string>();
            expected.Add("http://google.com");
            expected.Add("http://blogml.org");
            expected.Add("http://subkismet.com");

            List<string> actual;

            actual = target.RemoveDuplicates(results);

            TestGeneric<string>(actual, expected);
        }

        #region Helper methods

        private void TestGeneric<T>(List<T> actual, List<T> expected)
        {
            IEnumerator<T> actualEnumerator = actual.GetEnumerator();

            for (int i = 0; i < expected.Count; i++)
            {
                Assert.IsTrue(actualEnumerator.MoveNext());
                Assert.IsTrue(actualEnumerator.Current.Equals(expected[i]));
            }

            Assert.IsFalse(actualEnumerator.MoveNext());
        }

        #endregion
    }
}
